/*
This software is contributed by Hong Jun from the Open Source Software
of Chinese Academy of Sciences. 

This software is distributed under the terms of the GNU General Public License.

THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
APPLICABLE LAW.  EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE.  THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
IS WITH YOU.  SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
*/
package ac.cas.oss;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 *<b>Description:<b> Handle the HttpRequest, and response to that. 
 *
 *@author <a href="mailto:hongjun.bj@gmail.com">Hong Jun</a>
 *@since Nov 29, 2007 1:56:10 PM
 *@version 0.1
 */
public class RequestProcess extends HttpServlet {

	
	/**
	 * Handle the "GET" request from the web client.
	 * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		System.out.println("-----do get:");
		perform(request, response);
	}
	
	/**
	 * Handle the "POST" request from the web client.
	 * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		System.out.println("-----do post:");
		perform(request, response);
	}

	
	/**
	 * Get the parameters from the web client, and then put the responsing data to it.
	 * @param request
	 * @param response
	 */
	public void perform(HttpServletRequest request, HttpServletResponse response) {
		try {
			String latitute = request.getParameter("latitute");
			String longitute = request.getParameter("longitute");
			String urlStr = Constant.WeatherDataURL
					+ "ACode="+Constant.ACode+"&citytype="+Constant.CityType
					+"&unittype="+Constant.UnitType
					+ "&lat=" + latitute + "&long=" + longitute;
			String dataStr = new DataProcess().getWeatherData(urlStr);			
			response.setContentType("text/xml;charset=UTF-8");
			response.getWriter().print(dataStr);
			//System.out.println("----rss data:"+dataStr);
		} catch (Exception e) {
			System.out.println("-----perform error:");
			e.printStackTrace();
		}
	}
}
